package com.yusys.jin.blog.controller;

import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.ExceptionHandler;

import com.yusys.jin.blog.controller.ex.ControllerException;
import com.yusys.jin.blog.controller.ex.FileContentTypeException;
import com.yusys.jin.blog.controller.ex.FileIsEmptyException;
import com.yusys.jin.blog.controller.ex.FileSizeException;
import com.yusys.jin.blog.controller.ex.SavePictureException;
import com.yusys.jin.blog.controller.ex.VerifiedcodeIsNullException;
import com.yusys.jin.blog.controller.ex.VerifiedcodeIsWrongException;
import com.yusys.jin.blog.ex.DatabaseException;
import com.yusys.jin.blog.ex.FileIsSameWarmingException;
import com.yusys.jin.blog.ex.InsertException;
import com.yusys.jin.blog.ex.PasswordIsWrongException;
import com.yusys.jin.blog.ex.PictureNotExsitException;
import com.yusys.jin.blog.ex.SendTooFastException;
import com.yusys.jin.blog.ex.ServiceException;
import com.yusys.jin.blog.ex.UserConflictException;
import com.yusys.jin.blog.ex.UserHasExistException;
import com.yusys.jin.blog.ex.UserNotFoundException;
import com.yusys.jin.blog.ex.roleAlreadyExsitException;
import com.yusys.jin.blog.util.ResponseResult;

public class BaseController {
    /*
     * 处理请求时,用于表示操作正确的代码
     */
    public static final int HANDLE_SUCCESS = 200;
    /*
     * 操作错误
     */
    public static final int HANDLE_FAILED = 400;
    /*
     * 系统错误
     */
    public static final int SYSTEM_ERROR = 500;

    protected Integer getUidFromSession(HttpSession session) {
        return Integer.valueOf(session.getAttribute("uid").toString());
    }

    protected String getUsernameFromSession(HttpSession session) {
        return session.getAttribute("user").toString();
    }

    @ExceptionHandler({ServiceException.class, ControllerException.class})
    public ResponseResult<Void> handleException(Exception e) {
        ResponseResult<Void> rr = new ResponseResult<Void>();

        if (e instanceof InsertException) {
            rr.setState(SYSTEM_ERROR);
            rr.setMessage("系统异常，请联系管理员");
        } else if (e instanceof UserConflictException) {
            rr.setState(HANDLE_FAILED);
            rr.setMessage("用户已存在");
        } else if (e instanceof VerifiedcodeIsNullException) {
            rr.setState(HANDLE_FAILED);
            rr.setMessage("验证码不能为空");
        } else if (e instanceof VerifiedcodeIsWrongException) {
            rr.setState(HANDLE_FAILED);
            rr.setMessage("验证码输入错误");
        } else if (e instanceof UserNotFoundException) {
            rr.setState(HANDLE_FAILED);
            rr.setMessage("用户不存在");
        } else if (e instanceof PasswordIsWrongException) {
            rr.setMessage("密码错误");
            rr.setState(HANDLE_FAILED);
        } else if (e instanceof SendTooFastException) {
            rr.setMessage(e.getMessage());
            rr.setState(HANDLE_FAILED);
        } else if (e instanceof DatabaseException) {
            rr.setState(SYSTEM_ERROR);
            rr.setMessage("数据库操作失败请联系管理员");
        } else if (e instanceof UserHasExistException) {
            rr.setMessage(e.getMessage());
            rr.setState(HANDLE_FAILED);
        } else if (e instanceof FileIsEmptyException) {
            rr.setMessage("上传文件为空");
            rr.setState(HANDLE_FAILED);
        } else if (e instanceof FileContentTypeException) {
            rr.setMessage("文件类型不支持");
            rr.setState(HANDLE_FAILED);
        } else if (e instanceof FileSizeException) {
            rr.setMessage("文件过大，不支持");
            rr.setState(HANDLE_FAILED);
        } else if (e instanceof FileIsSameWarmingException) {
            rr.setState(401);
            rr.setMessage(e.getMessage());
        } else if (e instanceof SavePictureException) {
            rr.setState(SYSTEM_ERROR);
            rr.setMessage(e.getMessage());
        } else if (e instanceof roleAlreadyExsitException) {
            rr.setMessage(e.getMessage());
            rr.setState(400);
        } else if (e instanceof PictureNotExsitException) {
            rr.setState(401);
            rr.setMessage("头像图片不存在");
        }
        return rr;

    }
}
